<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link href="http://7xifrq.com1.z0.glb.clouddn.com/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<title>写文章</title>
</head>
<body>
	<div class="container-fluid">
		<div class="row">
			<div class="col-xs-6 col-md-9" style="padding: 0px">
				<input class="form-control" type="text" placeholder="标题" id="title">
			</div>
			<input class="col-xs-2 col-md-1 btn btn-default" type="button" value="清除" id="reset"> <input
				class="col-xs-2 col-md-1 btn btn-default" type="button" value="预览" id="preview"> <input
				class="col-xs-2 col-md-1 btn btn-default" type="button" value="发表" id="submit">
		</div>
	</div>
	<div id="epiceditor"></div>
	<div class="container-fluid login" style="display: none;">
		<div class="row">
			<div class="col-xs-4 col-md-4">
				<input class="form-control" type="text" id="user">
			</div>
			<div class="col-xs-4 col-md-4">
				<input class="form-control" type="password" id="pwd">
			</div>
			<div class="col-xs-4 col-md-4">
				<input class="btn btn-default" type="button" value="登录" id="login">
			</div>
		</div>
	</div>
</body>
<script type="text/javascript" src="http://7xifrq.com1.z0.glb.clouddn.com/common/tools.js"></script>
<script type="text/javascript" src="http://7xifrq.com1.z0.glb.clouddn.com/jquery.min.js"></script>
<script type="text/javascript" src="http://7xifrq.com1.z0.glb.clouddn.com/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://7xifrq.com1.z0.glb.clouddn.com/EpicEditor/js/epiceditor.min.js"></script>
<script type="text/javascript">
	var editorHeight = window.screen.availHeight / 2;
	if (isMobile.any()) {
		editorHeight = window.screen.availHeight / 7 * 3;
	}
	var opts = {
		basePath : 'http://7xifrq.com1.z0.glb.clouddn.com/EpicEditor',
		button : {
			bar : false
		},
		autogrow : {
			minHeight : editorHeight,
			maxHeight : editorHeight
		}
	}
	var editor = new EpicEditor(opts).load();
</script>
<script type="text/javascript" src="http://7xifrq.com1.z0.glb.clouddn.com/underscore-min.js"></script>
<script type="text/javascript" src="http://7xifrq.com1.z0.glb.clouddn.com/base64.js"></script>
<script type="text/javascript" src="http://7xifrq.com1.z0.glb.clouddn.com/github.js"></script>
<script type="text/javascript">
	window.btoa = function(data) {
		return base64.encode(wtf8.encode(data));
	};
	window.atob = function(data) {
		return wtf8.decode(base64.decode(data));
	}
	function post(user, pwd) {
		var github = new Github({
			username : user,
			password : pwd,
			auth : "basic"
		});
		var repo = github.getRepo("cmanlh", "cmanlh.github.io");
		repo.write('master', 'post/md/' + Date.now() + "-" + $("#title").val() + ".md", editor.exportFile(null, "text",
				false), 'post a new blog', function(err) {
			repo.write('master', 'post/html/' + Date.now() + "-" + $("#title").val(), editor.exportFile(null, "html",
					false), 'post a new blog', function(err, data) {
			});
		});
	}

	$("#submit").on("click", function(e) {
		var user, pwd;
		if (localStorage.githubAct) {
			var act = JSON.parse(localStorage.githubAct);
			post(atob(act.user), atob(act.pwd));
		} else {
			$(".login").show();
			return;
		}

	});

	$("#reset").on("click", function(e) {
		editor.editor.textContent = "";
	});

	$("#login").on("click", function(e) {
		localStorage.githubAct = JSON.stringify({
			user : btoa($("#user").val()),
			pwd : btoa($("#pwd").val())
		});
		$(".login").hide();
	});
</script>
</html>